package com.madarasan.xml;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.madarasan.xml.Message;

import android.app.Activity;
import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.util.Xml;

public class BaseFeedParser extends Activity {

	//static String feedUrlString = "http://dragos.madarasan.com/ski/android_news.rss.xml";
	static String feedUrlString = "http://dragos.madarasan.com/ski/ski.xml";
	
	// names of the XML tags
	static final String RSS = "rss";
	static final String CHANNEL = "channel";
	static final String ITEM = "item";
	
	static final String PUB_DATE = "pubDate";
	static final String DESCRIPTION = "description";
	static final String LINK = "link";
	static final String TITLE = "title";
	static final String TempMax = "tempmax";
	static final String TempMin = "tempmin";
	static final String Wind = "wind";
	static final String Snow = "snow";
	static final String Weather = "weather";
	
	
	private final URL feedUrl;

	protected BaseFeedParser(){
		try {
			this.feedUrl = new URL(feedUrlString);
		} catch (MalformedURLException e) {
			throw new RuntimeException(e);
		}
	}

	protected InputStream getInputStream() {
		try {
			return feedUrl.openConnection().getInputStream();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	
	/*private Thread checkUpdate = new Thread() {
        public void run() {
            try {
                URL updateURL = new URL("http://iconic.4feets.com/update");
                URLConnection conn = updateURL.openConnection();
                InputStream is = conn.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is);
                ByteArrayBuffer baf = new ByteArrayBuffer(50);

                int current = 0;
                while((current = bis.read()) != -1){
                    baf.append((byte)current);
                }

               
                html = new String(baf.toByteArray());
                mHandler.post(showUpdate);
            } catch (Exception e) {
            }
        }
    };*/

	
	
	
	
	
	public List<Message> parse() {
		final Message currentMessage = new Message();
		RootElement root = new RootElement(RSS);
		final List<Message> messages = new ArrayList<Message>();
		Element itemlist = root.getChild(CHANNEL);
		Element item = itemlist.getChild(ITEM);
		item.setEndElementListener(new EndElementListener(){
			public void end() {
				messages.add(currentMessage.copy());
			}
		});
		item.getChild(TITLE).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentMessage.setTitle(body);
			}
		});
		item.getChild(LINK).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentMessage.setLink(body);
			}
		});
		item.getChild(DESCRIPTION).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentMessage.setDescription(body);
			}
		});
		item.getChild(TempMax).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentMessage.setTempMax(body);
			}
		});
		
		item.getChild(TempMin).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentMessage.setTempMin(body);
			}
		});
		
		item.getChild(Wind).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentMessage.setWind(body);
			}
		});
		
		item.getChild(Snow).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentMessage.setSnow(body);
			}
		});
		
		item.getChild(Weather).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentMessage.setWeather(body);
			}
		});
		
		item.getChild(PUB_DATE).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentMessage.setDate(body);
			}
		});
		
		try {
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
		
		
		
		return messages;
	}
}